import { http } from './http'
import { product } from './url'

export function getProductDetail(productId: string) {
    const url = `${product.getProductDetail}/${productId}`
    return http.post(url, {}).then(res => {
        const product: HttpBkhc.Product = {
            code: '',
            name: '',
            brandList: [],
            specList: []
        }
        if (res.code === 200) {
            product.code = res.data?.productCode || '';
            product.name = res.data?.productName || '';
            if (res.data && res.data.brandList instanceof Array) {
                product.brandList = res.data.brandList.map((item: any) => {
                    return { code: item.brandCode, name: item.brandName }
                });
            }
            if (res.data && res.data.productSpecList instanceof Array) {
                product.specList = res.data.productSpecList.map((item: any) => {
                    return { code: item.specCode, name: item.specName }
                });
            }
        }
        return product
    }) as Promise<HttpBkhc.Product>
}

export function getProductPricesByBrand(params: any) {
    const url = product.getProductPricesByBrand;
    return http.get(url, params).then(res => {
        const productPrices = []
        if (res.code === 200) {
            if (res.data instanceof Array) {
                for (let item of res.data) {
                    if (item.goodsSpecList instanceof Array && item.priceList instanceof Array) {
                        const price = item.goodsSpecList.reduce((obj: any, item: any) => {
                            obj[item.specCode] = item.specValue
                            return obj
                        }, {})
                        price.price = item.priceList[0].price
                        price.goodsCode = item.goodsCode
                        productPrices.push(price)
                    }
                }
            }
        }
        return productPrices
    }) as Promise<HttpResponseData<HttpBkhc.ProductPrice[]>>
} 